<template>
  <div class="bk">
<div id="register">
  <div id="main-box">
  <h1>律师名片系统注册界面</h1>
  <div class="register">
  <el-form :model="UserForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
    <el-form-item label="用户名" prop="username">
      <el-input v-model="UserForm.username"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input v-model="UserForm.password" show-password></el-input>
    </el-form-item>
    <el-form-item label="年龄" prop="age">
      <el-input v-model="UserForm.age"></el-input>
    </el-form-item>
    <el-form-item label="联系电话" prop="phone">
      <el-input v-model="UserForm.phone"></el-input>
    </el-form-item>
<!--    <el-form-item label="学院" prop="college">-->
<!--      <el-select v-model="UserForm.college" >-->
<!--        <el-option v-for="(item,index) in categorys" :key="item.id" :label=item.college :value=item.college></el-option>-->
<!--      </el-select>-->
<!--    </el-form-item>-->

    <el-form-item label="身份" prop="rid">
      <el-select v-model="UserForm.rid" >
        <el-option label="客户" value="1"></el-option>
        <el-option label="律师" value="2"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="性别" prop="gender">
      <el-radio-group v-model="UserForm.gender">
        <el-radio label="男" value="1"></el-radio>
        <el-radio label="女" value="0"></el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm('ruleForm')">注册</el-button>
      <el-button @click="resetForm('ruleForm')">重置</el-button>
    </el-form-item>
  </el-form>

</div>
    <div style="display: flex;justify-content: flex-end">
      <el-link type="primary" style="margin-right: 10px" @click="tologin">已有账号，请登录</el-link>
    </div>
</div>
</div>
</div>
</template>


<script>
import axios from "axios";
import {querycategory, register} from "@/api/vcard";

export default {
  data() {
    return {
      categorys:[],
      UserForm: {
        username: '',
        password: '',
        age: '',
        college: '',
        phone: '',
        rid: '',
        gender: '',

      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
        ],
        age: [
          { required: true, message: '请输入年龄', trigger: 'blur' },
          { min: 0, max: 3, message: '年龄在0~100', trigger: 'blur' }
        ],
        college: [
          { required: true, message: '请输入学院', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入联系电话', trigger: 'blur' },
          { min: 11, max: 11, message: '请输入11位手机号', trigger: 'blur' }
        ],
        rid: [
          { required: true, message: '请输入身份', trigger: 'blur' },

        ],
        gender: [
          { required: true, message: '请输入性别', trigger: 'blur' },

        ]




      }
    };
  },
  created(){
    querycategory().then((res)=>{
      console.log(res);
      this.categorys = res.result
    })
  },
  methods: {
    //注册成功，跳转登录界面
    tologin(){
      this.$router.push('Login')
    },
    //点击注册按钮
    submitForm(formName) {
      const _this=this
      this.$refs[formName].validate((valid) => {
        if (valid) {
          register(_this.UserForm).then(resp => {
            if(!resp.success){
              this.$notify.error({
                title: '错误',
                message: resp.message
              });
            }else {
              console.log(resp),
                  this.$router.push({
                    name: 'Login'
                  })
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style scoped>
.bk{
  background: url("../images/bk.png");
  background-size: 100% 100%;
  width: 100%;
  height: 100%;
  position: fixed;

}
#register{

  width: 35%;
  text-align: center;
  margin: 0 auto;
  margin-top: 5%;
  background: #ffffff99;
  padding: 13px;

}
#main-box{
  background: #ffffff;
  padding :56px 0 40px;
}

.register{
  margin: 50px auto 20px;
  width: 400px;
  height: 450px;

}
#register h1 {
  color: black;
}
</style>